package com.baitengsoft.patentapimonitor.api.controller;

import com.baitengsoft.patentapimonitor.api.entity.WebUserinfoEntity;
import com.baitengsoft.patentapimonitor.api.model.WebMonAnalysisInfo;
import com.baitengsoft.patentapimonitor.api.model.WebMonInvalidAnalysisInfo;
import com.baitengsoft.patentapimonitor.api.model.dto.WebApplicantTypeInfoDto;
import com.baitengsoft.patentapimonitor.api.model.dto.WebMonAnalysisInfoDto;
import com.baitengsoft.patentapimonitor.api.model.dto.WebMonInvalidAnalysisInfoDto;
import com.baitengsoft.patentapimonitor.api.service.PacPatentinfoOutService;
import com.baitengsoft.patentapimonitor.api.service.PacPatentinfoService;
import com.baitengsoft.patentapimonitor.api.service.WebExcelExportService;
import com.baitengsoft.patentapimonitor.common.utils.Res;
import com.baitengsoft.patentapimonitor.common.utils.WebExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 监控分析导出
 * @author Chen 2018/1/15
 */
@RestController
@RequestMapping("api/v1/web/monanalysisexport")
@Api(value = "前台-专利监控-监控分析导出",description = "监控分析导出")
public class WebMonAnalysisExportController extends AbstractController {
    @Autowired
    private PacPatentinfoService pacPatentinfoService;
    @Autowired
    private PacPatentinfoOutService pacPatentinfoOutService;
    @Autowired
    private WebExcelExportService webExcelExportService;

    /**
     * 专利权转入WebAreaStatisticInfo
     */
    @ApiOperation(value ="转入")
    @GetMapping("/in")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "sort", value = "排序字段", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "order", value = "排序方式[desc asc]", required = false)
    })
    public Res getInList(String sort, String order, HttpServletResponse response){
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo ==null || userInfo.getPlatformid()==null){
            return Res.error("登录信息失效");
        }
        List<WebMonAnalysisInfo> infos = pacPatentinfoService.getInList(userInfo.getPlatformid(), order, sort);
        if (infos == null || infos.size()<1)    return Res.error("没有可以导出的信息");
        List<WebMonAnalysisInfoDto> webDtos = webExcelExportService.getWebMonAnalysisInfoDto(infos);
        return WebExcelUtils.exportGeneralExcelFile(
                webDtos,
                response,
                "专利权转入分析.xls",
                "专利权转入分析");
    }

    /**
     * 专利权转出
     */
    @ApiOperation(value ="转出")
    @GetMapping("/out")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "sort", value = "排序字段", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "order", value = "排序方式[desc asc]", required = false)
    })
    public Res getOutList(String sort, String order,HttpServletResponse response ){
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo ==null || userInfo.getPlatformid()==null){
            return Res.error("登录信息失效");
        }
        List<WebMonAnalysisInfo> infos = pacPatentinfoService.geOutList(userInfo.getPlatformid(), order, sort);
        if (infos == null || infos.size()<1)    return Res.error("没有可以导出的信息");
        List<WebMonAnalysisInfoDto> webDtos = webExcelExportService.getWebMonAnalysisInfoDto(infos);
        return WebExcelUtils.exportGeneralExcelFile(
                webDtos,
                response,
                "专利权转出分析.xls",
                "专利权转出分析");
    }

    /**
     * 专利权失效
     */
    @ApiOperation(value ="失效")
    @GetMapping("/invalid")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "sort", value = "排序字段", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "order", value = "排序方式[desc asc]", required = false)
    })
    public Res getInvalidList(String sort, String order,HttpServletResponse response){
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo ==null || userInfo.getPlatformid()==null){
            return Res.error("登录信息失效");
        }
        List<WebMonInvalidAnalysisInfo> infos = pacPatentinfoService.geInvalidList(userInfo.getPlatformid(), order, sort);
        if (infos == null || infos.size()<1)    return Res.error("没有可以导出的信息");
        List<WebMonInvalidAnalysisInfoDto> webDtos = webExcelExportService.getWebMonInvalidAnalysisInfoDto(infos);
        return WebExcelUtils.exportGeneralExcelFile(
                webDtos,
                response,
                "专利权转出分析.xls",
                "专利权转出分析");
    }
}
